<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item :label="$t('机台名称')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gameId">
              <j-search-select-tag :placeholder="$t('请选择机台名称')" v-model="model.gameId" dict="bis_game,name,id" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item :label="$t('工单状态')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskStatus">
              <j-search-select-tag :placeholder="$t('请选择工单状态')" v-model="model.taskStatus"
                dict="bis_game_repair.task_status" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('问题描述')" :labelCol="labelCol1" :wrapperCol="wrapperCol1" prop="description">
              <a-textarea v-model="model.description" :placeholder="$t('请输入问题描述')" rows="4"></a-textarea>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('故障图片')" :labelCol="labelCol1" :wrapperCol="wrapperCol1" prop="repairImageList">
              <j-upload v-model="model.repairImageList" fileType="image"></j-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('维修员')" :labelCol="labelCol1" :wrapperCol="wrapperCol1" prop="repairBy">
              <a-select v-model="model.repairBy" :placeholder="$t('请选择维修员')">
                <a-select-option v-for="(item, index) in userList" :value="item.id" :key="index">{{
                  item.realname
                }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('维修信息')" :labelCol="labelCol1" :wrapperCol="wrapperCol1" prop="repairInfo">
              <a-textarea v-model="model.repairInfo" :placeholder="$t('请输入维修信息')" rows="4"></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage';
import store from '@/store/'

export default {
  name: 'BisGameRepairForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        taskStatus: 0,
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      labelCol1: {
        xs: { span: 10 },
        sm: { span: 3 },
      },
      wrapperCol1: {
        xs: { span: 14 },
        sm: { span: 20 },
      },
      confirmLoading: false,
      validatorRules: {
        gameId: [
          { required: true, message: this.$t('请选择机台名称') },
        ],
        taskStatus: [
          { required: true, message: this.$t('请选择工单状态') },
        ],
        description: [
          { required: true, message: this.$t('请输入问题描述') },
        ],
        repairImageList: [
          { required: true, message: this.$t('请上传图片') },
        ],
      },
      editRules: {
        repairBy: [
          { required: true, message: this.$t('请选择维修人') },
        ],
        repairInfo: [
          { required: true, message: this.$t('请输入维修信息') },
        ],
      },
      url: {
        add: "/game/bisGameRepair/add",
        edit: "/game/bisGameRepair/edit",
        queryById: "/game/bisGameRepair/queryById"
      },
      userList: [],
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add() {
      this.edit(this.modelDefault);
    },
    edit(record, type) {
      getAction('/sys/user/list').then((res) => {
        if (res.success) {
          this.userList = res.result.records
          this.userList.forEach((col) => {
            col.id = col.realname
          })
        }
      })
      this.model = Object.assign({}, record);
      this.visible = true;
      if (type === 1) {
        this.validatorRules = {
          ...this.validatorRules,
          ...this.editRules
        };
        this.model.repairBy = store.getters.userInfo.realname
      } else {
        this.validatorRules = {
          gameId: [
            { required: true, message: this.$t('请选择机台') },
          ],
          taskStatus: [
            { required: true, message: this.$t('请选择工单状态') },
          ],
          description: [
            { required: true, message: this.$t('请输入问题描述') },
          ],
          repairImageList: [
            { required: true, message: this.$t('请上传图片') },
          ],
        }
      }
      let list = []
      this.model.imageList.forEach((col) => {
        list.push(col.path)
      })
      this.model.repairImageList = list.join(',')
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          let repairImageList = []
          if (this.model.repairImageList) {
            let list = []
            list = this.model.repairImageList.split(',')
            list.forEach((lt) => {
              let obj = {}
              obj.path = lt
              repairImageList.push(obj)
            })
          }
          this.model.imageList = repairImageList;
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>